﻿using System;
using System.Collections;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio2013.Modelo;
using Obligatorio2013.Clases;
using System.Collections.Generic;

namespace Obligatorio2013.Pages
{
    public partial class UserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["logged"] == true)
            {


                if (!Page.IsPostBack)
                {
                    if (Session["controller"] == null)
                    {
                        Session["controller"] = Sistema.Instance;

                    }

                }

                Sistema controller = Session["controller"] as Sistema;
                List<Users> users = controller.getUsers();
                if (users != null && users.Count != 0)
                {
                    this.gvUsers.DataSource = users;
                    this.gvUsers.DataBind();
                }
                else
                {
                    this.lblMessage.Text = "No hay usuarios registrados en el sistema.";
                }
            }
            else
            {
                Response.Redirect("~/Pages/Private/LoginForm.aspx");
            }


        }

        protected void rbtAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtAll.Checked == true)
            {
                Sistema controller = Session["controller"] as Sistema;
                List<Users> users = controller.getUsersByStatus(true, false);
                if (users != null && users.Count != 0)
                {
                    this.lblMessage.Text = "";
                    this.gvUsers.DataSource = users;
                    this.gvUsers.DataBind();
                }
                else
                {
                    this.lblMessage.Text = "No hay usuarios registrados en el sistema.";
                    this.gvUsers.DataSource = null;
                    this.gvUsers.DataBind();
                }
            }

        }

        protected void rbtActive_CheckedChanged(object sender, EventArgs e)
        {

            if (rbtActive.Checked == true)
            {

                Sistema controller = Session["controller"] as Sistema;
                List<Users> users = controller.getUsersByStatus(false, true);
                if (users != null && users.Count != 0)
                {
                    this.lblMessage.Text = "";
                    this.gvUsers.DataSource = users;
                    this.gvUsers.DataBind();
                }
                else
                {
                    this.lblMessage.Text = "No hay usuarios activos registrados en el sistema.";
                    this.gvUsers.DataSource = null;
                    this.gvUsers.DataBind();
                }
            }
        }

        protected void rbtInactive_CheckedChanged(object sender, EventArgs e)
        {

            if (rbtInactive.Checked == true)
            {

                Sistema controller = Session["controller"] as Sistema;
                List<Users> users = controller.getUsersByStatus(false, false);
                if (users != null && users.Count != 0)
                {
                    this.lblMessage.Text = "";
                    this.gvUsers.DataSource = users;
                    this.gvUsers.DataBind();
                }
                else
                {
                    this.lblMessage.Text = "No hay usuarios suspendidos registrados en el sistema.";
                    this.gvUsers.DataSource = null;
                    this.gvUsers.DataBind();
                }
            }

        }



        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvUsers.EditIndex = e.NewEditIndex;
            GridViewRow row = (GridViewRow)gvUsers.Rows[gvUsers.EditIndex];
            string usrId = gvUsers.Rows[gvUsers.EditIndex].Cells[4].Text;
            Session.Add("editUsr", usrId);
            //Server.Transfer("~/Pages/UserEditForm.aspx", true);
            Response.Redirect("~/Pages/EditUserForm.aspx");
            //string url = String.Format("~/Pages/EditUserForm.aspx?p1={0}", usrId);
            //Response.Redirect(url);

        }
    }
}